package com.quanxiaoha.xiaohashu.auth.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: jingyan_liu
 * @CreateTime: 2025-07-09
 * @Description:
 * @Version: 1.0
 */


@Configuration
@Slf4j
public class AliyunSmsClientConfig {
	
	private final AliyunAccessKeyProperties aliyunAccessKeyProperties;
	
	public AliyunSmsClientConfig(AliyunAccessKeyProperties aliyunAccessKeyProperties) {
		this.aliyunAccessKeyProperties = aliyunAccessKeyProperties;
	}
	
	@Bean
	public Client smsClient() {
		try {
			Config config = new Config()
					// 必填
					.setAccessKeyId(aliyunAccessKeyProperties.getAccessKeyId())
					// 必填
					.setAccessKeySecret(aliyunAccessKeyProperties.getAccessKeySecret());
			
			// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
			config.endpoint = "dysmsapi.aliyuncs.com";
			
			return new Client(config);
		} catch (Exception e) {
			log.error("初始化阿里云短信发送客户端错误: ", e);
			return null;
		}
	}
}
